package com.atguigu.gmall.product.controller;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.model.product.*;
import com.atguigu.gmall.product.service.ManageService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 商品管理
 */
@Api(tags = "后台管理中心")
@RestController
//@CrossOrigin
@RequestMapping("/admin/product") //公共路径
public class ManageController {

    @Autowired
    private ManageService manageService;

    //1、获取1级分类
    @GetMapping("/getCategory1")
    @ApiOperation("获取一级分类")
    public Result getCategory1(){
        List<BaseCategory1> baseCategory1List = manageService.getCategory1();
        return Result.ok(baseCategory1List);
    }

    //2、获取2级分类
    @ApiOperation("获取2级分类By1级分类")
    @GetMapping("/getCategory2/{category1Id}")
    public Result getCategory2ByCategory1Id(@PathVariable("category1Id") Long category1Id){
        List<BaseCategory2> baseCategory2List = manageService.getCategory2ByCategory1Id(category1Id);
        return Result.ok(baseCategory2List);
    }

    //3、获取3级分类
    @ApiOperation("获取3级分类By2级分类")
    @GetMapping("/getCategory3/{category2Id}")
    public Result getCategory3ByCategory2Id(@PathVariable("category2Id") Long category2Id){
        List<BaseCategory3> baseCategory3List = manageService.getCategory3ByCategory2Id(category2Id);
        return Result.ok(baseCategory3List);
    }


    //4、根据分类id获取平台属性
    @ApiOperation("根据分类id获取平台属性")
    @GetMapping("/attrInfoList/{category1Id}/{category2Id}/{category3Id}")
    public Result attrInfoList(
            @PathVariable("category1Id")Long category1Id,
            @PathVariable("category2Id")Long category2Id,
            @PathVariable("category3Id")Long category3Id){
        return Result.ok(manageService.attrInfoList(category1Id,category2Id,category3Id));
    }

    //5、添加平台属性
    @PostMapping("/saveAttrInfo")
    @ApiOperation("添加平台属性")
    public Result saveAttrInfo(@RequestBody BaseAttrInfo baseAttrInfo){
        manageService.saveAttrInfo(baseAttrInfo);
        return Result.ok();
    }


    //6、查询SPU分页列表
    @GetMapping("/{page}/{limit}")
    @ApiOperation("查询SPU分页列表")
    public Result spuPage(
            @PathVariable(name = "page") Integer page,
            @PathVariable(name = "limit") Integer limit,
            Long category3Id
    ){
        IPage<SpuInfo> p = manageService.spuPage(page,limit,category3Id);

        return Result.ok(p);
    }

    //7、去SPU添加页面 加载所有品牌
    @GetMapping("/baseTrademark/getTrademarkList")
    @ApiOperation("去SPU添加页面 加载所有品牌")
    public Result getTrademarkList(){
        List<BaseTrademark> baseTrademarkList = manageService.getTrademarkList();
        return Result.ok(baseTrademarkList);
    }

    //8、获取销售属性
    @GetMapping("/baseSaleAttrList")
    public Result baseSaleAttrList(){
        List<BaseSaleAttr> baseSaleAttrList = manageService.baseSaleAttrList();
        return Result.ok(baseSaleAttrList);
    }

    //9、添加spu
    @PostMapping("/saveSpuInfo")
    public Result saveSpuInfo(@RequestBody SpuInfo spuInfo){
        manageService.saveSpuInfo(spuInfo);
        return Result.ok();
    }

    //10、根据spuId获取图片列表
    @GetMapping("/spuImageList/{spuId}")
    public Result spuImageList(@PathVariable(name = "spuId") Long spuId){
        List<SpuImage> spuImageList = manageService.spuImageList(spuId);
        return Result.ok(spuImageList);
    }

    //11、根据spuId获取销售属性
    @GetMapping("/spuSaleAttrList/{spuId}")
    public Result spuSaleAttrList(@PathVariable(name = "spuId") Long spuId){
        List<SpuSaleAttr> spuSaleAttrList = manageService.spuSaleAttrList(spuId);
        return Result.ok(spuSaleAttrList);
    }

    //12、保存SKU
    @PostMapping("/saveSkuInfo")
    public Result saveSkuInfo(@RequestBody SkuInfo skuInfo){
        manageService.saveSkuInfo(skuInfo);
        return Result.ok();
    }

    //13、获取sku分页列表
    @GetMapping("/list/{page}/{limit}")
    public Result skuList(@PathVariable(name = "page")Integer page,
                          @PathVariable(name = "limit")Integer limit){
        //查询分页对象
        IPage<SkuInfo> p = manageService.skuList(page,limit);
        return Result.ok(p);
    }

    //14、上架
    @GetMapping("/onSale/{skuId}")
    public Result onSale(@PathVariable(name = "skuId")Long skuId){
        manageService.onSale(skuId);
        return Result.ok();
    }

    //15、下架
    @GetMapping("/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable(name = "skuId")Long skuId){
        manageService.cancelSale(skuId);
        return Result.ok();
    }

}
